package com.bluefox.hospital.service.impl;

import com.bluefox.common.web.exception.CustomRuntimeException;
import com.bluefox.hospital.model.Image;
import com.bluefox.hospital.repository.ImageRepository;
import com.bluefox.hospital.service.ImageService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ImageService 实现类
 */
@Service
@Transactional
public class ImageServiceImpl implements ImageService {

    @Autowired
    private ImageRepository imageRepository;

    @Override
    @Transactional(readOnly = true)
    public Image get(@NonNull Long id) {
        final Image model = imageRepository.findById(id).get();
        if (model == null) {
            throw new CustomRuntimeException("404", String.format("查找的资源[%s]不存在.", id));
        }
        return model;
    }

    @Override
    public Image create(Image model) {
        // TODO: 业务逻辑
        return imageRepository.save(model);
    }

    @Override
    public Image update(Image model) {
        // TODO: 业务逻辑
        return imageRepository.save(model);
    }

    @Override
    public void delete(@NonNull Long id) {
        // TODO: 业务逻辑
        imageRepository.deleteById(id);
    }

    @Override
    public List<Image> getImageTop10ByTypeOne() {
        return imageRepository.findTop10ByTypeOrderByAutoOrderDescCreatedDateDesc("1");
    }

    @Override
    public List<Image> getMidImg() {
        return imageRepository.findTop1ByTypeOrderByCreatedDateDesc("2");
    }

    @Override
    public int findMaxOrder() {
        Object obj = imageRepository.findMaxOrder();
        if (obj == null) {
            return 1;
        } else {
            return (Integer) obj + 1;
        }
    }
    @Override
    public int findMaxOrderByTypeOne() {
        Object obj = imageRepository.findMaxOrderByTypeOne();
        if (obj == null) {
            return 1;
        } else {
            return (Integer) obj + 1;
        }
    }

}
